import { createRouter, createWebHistory } from 'vue-router'
import Cookies from 'js-cookie'
const routes = [{
        path: '/',
        redirect: '/login',
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('../views/user/Login.vue')
    },
    {
        path: '/ac/:or',
        name: 'ac',
        component: () =>
            import ('../views/user/ac.vue')

    },
    {
        path: '/test',
        name: 'test',
        component: () =>
            import ('../views/test.vue')

    },
    {
        path: '/chat',
        name: 'chat',
        component: () =>
            import ('../views/layout/chat.vue'),
        redirect: '/chatweb',

        children: [{
                path: '/chatweb',
                component: () =>
                    import ('../views/chat/chatweb.vue')
            },
            {
                path: '/friend',
                component: () =>
                    import ('../views/chat/friends.vue')

            },
            {
                path: '/setup',
                component: () =>
                    import ('../views/chat/setup.vue')

            },
            {
                path: '/chat/userDetail',
                component: () =>
                    import ('@/views/user/userDetail.vue')

            },
        ]
    }

]
const router = createRouter({
    history: createWebHistory(
        import.meta.env.BASE_URL),
    routes

})
router.beforeEach(async(to, from, next) => {

    const token = Cookies.get('IM-token');

    if (to.path !== '/' && to.path !== '/login' && !token) {
        return next('/login');
    }
    next();


})
export const nowToken = Cookies.get('IM-token')
export { Cookies }
export default router